""" System imports """
import logging
import unittest

""" Other modules """

""" Game """

""" Constants """

""" Set up logging """
_log = logging.getLogger("epicquest.eventrouter")
_log.debug("Initializing Event Router")

class EventRouter():
    
    """ All the different types of events """
    PAUSE = "pause"
    """ player - Player that moved 
        coords - New coords
    """
    PLAYER_MOVE = "playermove"
    
    """ Class to handle all event routing
    """    
    def __init__(self):
        self.subscribers = []
    
    """ Subscribe to an event
    """
    def subscribe(self, subscriber, type, handler, **config):
#        subscriber = config.get('subscriber', None)
#        type = config.get('type', eventpanel.PAUSE)
#        handler = config.get('handler', None)
        id = config.get('id', None)
        
        self.subscribers.append({'subscriber':subscriber,'type':type,'handler':handler,'id':id})
        #self.subscribers.sort()
    
    """ Fire an event
    """
    def fire_event(self, caller, type, **params):
        for subscriber in self.subscribers:
            if subscriber.get('type') == type:
                # If the subscriber has an id, we enforce 
                # the requirement that the caller has the same id. 
                if subscriber.has_key('id') and subscriber.get('id') is not None and hasattr(caller, 'id'):
                    if caller.id == subscriber.get('id'):
                        subscriber.get('handler')(caller, type, **params)
                else:
                    subscriber.get('handler')(caller, type, **params)

# Module level instance. Don't make any more. That would be silly
eventpanel = EventRouter()


class EventTest(unittest.TestCase):
    
    def setUp(self):
        pass
    
if __name__ == "__main__":
    unittest.main()

